﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;

namespace iMusik.Mp3ZingOnline
{
    public class Mp3Zing
    {
        private ZingHome _Home;
        private ZingSearch _Search;
        private ZingTop _Top;
        private ZingTop100 _Top100;
        private ZingVideoHot _HotVideo;

        public ZingVideoHot HotVideo
        {
            get { return _HotVideo; }
            private set { _HotVideo = value; }
        }
        public ZingTop100 Top100
        {
            get { return _Top100; }
            private set { _Top100 = value; }
        }
        public ZingSearch Search
        {
            get { return _Search; }
            private set { _Search = value; }
        }
        public ZingHome Home
        {
            get { return _Home; }
            private set { _Home = value; }
        }
        public ZingTop Top
        {
            get { return _Top; }
            private set { _Top = value; }
        }

        private bool _ISetTop;
        private bool _IsSetHome;
        private bool _IsSetTop100;
        private bool _IsSetHotVideo;
        public Mp3Zing()
        {
            _ISetTop = false;
            _IsSetHome = false;
            _IsSetTop100 = false;
            _IsSetHotVideo = false;
        }

        public async Task SetTop()
        {
            if (_ISetTop)
                return;
            try
            {
                _Top = new ZingTop("http://m.mp3.zing.vn/bang-xep-hang/index.html");
                await _Top.SetTopVN();
                await _Top.SetTopUS();
                await _Top.SetTopKR();        
                _ISetTop = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
           
        }

        public async Task SetHome()
        {
            if (_IsSetHome)
                return;
            try
            {
                _Home = new ZingHome();
                await _Home.SetAllInformation();
                _IsSetHome = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetSearch(string key)
        {
            try
            {
                _Search = new ZingSearch(key);
                await _Search.SetAllInformation();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetTop100()
        {
            if (_IsSetTop100)
                return;
            try
            {
                _Top100 = new ZingTop100(@"http://m.mp3.zing.vn");
                await _Top100.SetTop100NhacTre();
                _IsSetHome = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetHotVideo()
        {
            if (_IsSetHotVideo)
                return;
            try
            {
                _HotVideo = new ZingVideoHot();
                await _HotVideo.SetAllInformation();
                _IsSetHotVideo = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #region Ho tro
        public static string ConvertStringAsciiToUtf8(string myString)
        {
            string rs = Regex.Unescape(myString);
            return rs;
            
        }
        public static Task<string> DownloadString(Uri uri)
        {
            try
            {
                var task = new TaskCompletionSource<string>();
                var client = new WebClient();
                client.DownloadStringCompleted += (s, e) =>
                {
                    if (e.Error == null)
                    {
                        task.SetResult(e.Result);
                    }
                    else
                    {
                        task.SetException(new Exception("NetError1908"));
                    }

                };
                client.DownloadStringAsync(uri);

                return task.Task;
            }
            catch(Exception ex)
            {
                throw (new Exception("NetError1908"));
            }
        }
       
        #endregion

    }
}
